<template>
  <div class="addBrand-container">
    <div class="container">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      
            <el-form-item label="账号" prop="username">
            <el-input v-model="ruleForm.username" placeholder="‘请输入账号"></el-input>
            </el-form-item>

            <el-form-item label="员工姓名" prop="name">
            <el-input v-model="ruleForm.name" placeholder="请输入员工姓名"></el-input>
            </el-form-item>

            <el-form-item label="手机号" prop="phone">
            <el-input v-model="ruleForm.phone" placeholder="手机号"></el-input>
            </el-form-item>

            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="ruleForm.sex">
                <el-radio label="男"></el-radio>
                <el-radio label="女"></el-radio>
              </el-radio-group>
            </el-form-item> 
        
            <el-form-item label="身份证号" prop="idNumber">
            <el-input v-model="ruleForm.idNumber" placeholder="身份证号"></el-input>
            </el-form-item>
        </el-form>
        <div class="subBox address">
          <el-button type="primary" @click="submitForm('ruleForm', false)"> 保存</el-button>

          <el-button v-if="optType == 'add'"
                     type="primary"
                     @click="submitForm('ruleForm', true)">
            保存并继续添加
          </el-button>
          
          <el-button @click="() => $router.push('/employee')">返回</el-button>
        </div>
      
    </div>
  </div>
</template>

<script lang="ts">
import { queryEmployeeById, addEmployee, editEmployee } from '@/api/employee'



export default  {
  data(){
    return{
      optType: '',
      ruleForm: {
        username: '',
          name: '',
          phone: '',
          sex: '1',
          idNumber: ''
        },
        rules: {
          username: [
            { required: true, message: '请输入账号', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入员工姓名', trigger: 'blur' }
          ],
          phone: [
            { required: true,  trigger: 'blur', validator: (rule,value,callback)=>{ 
                if(value===''||(!/^1(3|4|5|6|7|8|9)\d{9}$/.test(value))){
                  callback(new Error('请输入正确的手机号码!'))
                }else{
                  callback()
                }
             }
            }
          ],
          idNumber: [
            { required: true,  trigger: 'blur', validator: (rule,value,callback)=>{ 
                if(value===''||(!/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(value))){
                  callback(new Error('请输入正确的身份证号码!'))
                }else{
                  callback()
                }
             }
            }
          ]
        }
    }
  },
  created(){
    this.optType=this.$route.query.id ? 'update' : 'add'

    if(this.optType === 'update'){
      //修改操作，查询员工信息然后回显到页面
      queryEmployeeById(this.$route.query.id).then(res=>{
        if(res.data.code === 1){
          this.ruleForm=res.data.data
        }
      })
    }
  },
  methods: {
    submitForm(formName,isContinue){
      //进行表单校验
      this.$refs[formName].validate((validate)=>{ 
        if(validate){
            //ajax请求提交表单数据到后台
          if(this.optTyp === 'add'){//新增操作
            addEmployee(this.ruleForm).then((res)=>{
              if(res.data.code === 1){
                this.$message.success('员工添加成功')
                if(isContinue){
                 this.ruleForm={
                  username: '',
                  name: '',
                  phone: '',
                  sex: '1',
                  idNumber: ''
                 }
              }else{
                this.$router.push('/employee')
               } 
              }else{
                this.$message.error(res.data.msg)
              }
            })
           }else{//修改操作
               editEmployee(this.ruleForm).then(res=>{
              if(res.data.code === '1'){
                this.$message.success(res.data.data.username+'员工数据修改成功！')
                this.$router.push('/employee')
              }
            })
           }
          }

      })
    }
  }


}
  
</script>


<style lang="scss" scoped>
.addBrand {
  &-container {
    margin: 30px;
    margin-top: 0px;
    .HeadLable {
      background-color: transparent;
      margin-bottom: 0px;
      padding-left: 0px;
    }
    .container {
      position: relative;
      z-index: 1;
      background: #fff;
      padding: 30px;
      border-radius: 4px;
      // min-height: 500px;
      .subBox {
        padding-top: 30px;
        text-align: center;
        border-top: solid 1px $gray-5;
      }
    }
    .idNumber {
      margin-bottom: 39px;
    }

    .el-form-item {
      margin-bottom: 29px;
    }
    .el-input {
      width: 293px;
    }
  }
}
</style>
